package com.fulu.game.bi.service.impl.room;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.common.util.lang.StringUtils;
import com.fulu.game.bi.entity.bo.channel.VoiceGuildStatListBO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.room.VoiceGuildConsumerVO;
import com.fulu.game.bi.entity.vo.room.VoiceGuildVO;
import com.fulu.game.bi.hbase.HBaseVoiceGuildStatDAO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VoiceGuildServiceImpl extends BaseVoiceService {

    @Autowired
    private HBaseVoiceGuildStatDAO hBaseVoiceGuildStatDAO;


    public PageInfo<VoiceGuildVO> list(VoiceGuildStatListBO listBO) {
        listBO.setTableName(tableNameGuild.get(listBO.getQueryType()));
        listBO.setPageNum((listBO.getPageNum() - 1) * listBO.getPageSize());
        listBO.setGuildName(StringUtils.isBlank(listBO.getGuildName()) ? listBO.getGuildName() : "%" + listBO.getGuildName() + "%");
        List<VoiceGuildVO> list = hBaseVoiceGuildStatDAO.list(listBO);
        Integer count = hBaseVoiceGuildStatDAO.count(listBO);
        list = list.stream().map(map -> {
            map.setQueryType(listBO.getQueryType());
            return map;
        }).collect(Collectors.toList());
        PageInfo<VoiceGuildVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        return pageInfo;
    }

    public void export(VoiceGuildStatListBO bo) {
        String fileName = "语音互动公会";
        LinkedHashMap<String, Mapper<VoiceGuildVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(bo.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("公会ID", v -> v.getGuildCode());
        map.put("公会名称", v -> "\"" + v.getGuildName() + "\"");
        map.put("公会类型", v -> v.getGuildType());
        map.put("钻石消费", v -> v.getVirtualMoneyAll().toString());
        map.put("消费人数", v -> v.getConsumeUserNum() + "");
        map.put("消费次数", v -> v.getConsumeNum() + "");
        map.put("魅力值流水", v -> v.getCharmMoney().toString());
        map.put("进厅人数", v -> v.getEnterRoomUserNum() + "");
        map.put("进厅次数", v -> v.getEnterRoomNum() + "");
        map.put("新增收藏", v -> v.getAddCollectNum() + "");
        map.put("用户平均停留时长（min）", v -> v.getUserStayTimeAvg().toString());
        map.put("停留 > 5 分钟用户数", v -> v.getStayFiveMinUserNum() + "");
        map.put("互动用户数", v -> v.getInteractUserNum() + "");
        Querier querier = (num) -> list(bo);
        CsvExportUtil.export(100, fileName, map, querier);
    }

    public PageInfo<VoiceGuildConsumerVO> consumerList(VoiceGuildStatListBO listBO) {
        listBO.setTableName(tableNameGuild.get(listBO.getQueryType()));
        listBO.setPageNum((listBO.getPageNum() - 1) * listBO.getPageSize());
        listBO.setGuildName(StringUtils.isBlank(listBO.getGuildName()) ? listBO.getGuildName() : "%" + listBO.getGuildName() + "%");
        List<VoiceGuildConsumerVO> list = hBaseVoiceGuildStatDAO.consumerList(listBO);
        Integer count = hBaseVoiceGuildStatDAO.count(listBO);
        list = list.stream().map(map -> {
            map.setQueryType(listBO.getQueryType());
            return map;
        }).collect(Collectors.toList());
        PageInfo<VoiceGuildConsumerVO> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count == null ? 0 : count);
        return pageInfo;
    }

    public void consumerExport(VoiceGuildStatListBO bo) {
        String fileName = "语音互动公会消费明细";
        LinkedHashMap<String, Mapper<VoiceGuildConsumerVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(bo.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("公会ID", v -> v.getGuildCode());
        map.put("公会名称", v -> "\"" + v.getGuildName() + "\"");
        map.put("购买礼物人数", v -> v.getBuyGiftUserNum() + "");
        map.put("购买礼物次数", v -> v.getBuyGiftNum() + "");
        map.put("购买礼物魅力值流水", v -> v.getBuyGiftMoneyAll().toString());
        map.put("礼盒礼物人数", v -> v.getBuyBoxUserNum() + "");
        map.put("礼盒礼物次数", v -> v.getBuyBoxNum() + "");
        map.put("礼盒礼物魅力值流水", v -> v.getBuyBoxMoneyAll().toString());
        map.put("红包礼物人数", v -> v.getBuyRedbagUserNum() + "");
        map.put("红包礼物次数", v -> v.getBuyRedbagNum() + "");
        map.put("红包礼物魅力值流水", v -> v.getBuyRedbagMoneyAll().toString());
        map.put("背包礼物人数", v -> v.getBagGiftUserNum() + "");
        map.put("背包礼物次数", v -> v.getBagGiftNum() + "");
        map.put("背包礼物魅力值流水", v -> v.getBagGiftMoneyAll().toString());
        map.put("粉丝团人数", v -> v.getFansUserNum() + "");
        map.put("粉丝团次数", v -> v.getFansNum() + "");
        map.put("粉丝团消费金额", v -> v.getFansMoneyAll().toString());
        Querier querier = (num) -> consumerList(bo);
        CsvExportUtil.export(100, fileName, map, querier);
    }
}
